package com.bizihang.common.security.core.code;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;


/**
 * 验证码信息封装类
 *
 * @author 毕子航 951755883@qq.com
 * @date 2018/10/19
 */
@Getter
@Setter
public class ValidateCode {
	/**
	 * 验证码
	 */
	private String code;

	/**
	 * 验证码类型
	 */
	private String type;

	/**
	 * 过期时间
	 */
	@JsonDeserialize(using = LocalDateTimeDeserializer.class)
	@JsonSerialize(using = LocalDateTimeSerializer.class)
	private transient LocalDateTime expireTime;

	public ValidateCode() {
	}


	/**
	 * @param code         验证码
	 * @param expireDuring 过期时间间隔
	 */
	public ValidateCode(String code, int expireDuring) {
		this.code = code;
		this.expireTime = LocalDateTime.now().plusSeconds(expireDuring);
	}

	/**
	 * @param code       验证码
	 * @param expireTime 过期时间
	 */
	public ValidateCode(String code, LocalDateTime expireTime) {
		this.code = code;
		this.expireTime = expireTime;
	}

	@JsonIgnore
	public boolean isExpired() {
		return LocalDateTime.now().isAfter(expireTime);
	}
}
